import request from './request'

// ==================== 用户登录相关 ====================
// 用户登录
export const login = (data) => {
  return request({
    url: '/api/user/login',
    method: 'post',
    data
  })
}

// 登录状态验证
export const loginStatus = () => {
  return request({
    url: '/api/user/login/status',
    method: 'get'
  })
}

// 退出登录
export const logout = () => {
  return request({
    url: '/api/user/logout',
    method: 'get'
  })
}

// ==================== 系管理 ====================
// 获取系列表（分页）
export const getDepartmentPage = (page, params = {}) => {
  return request({
    url: `/api/admin/department/page/${page}`,
    method: 'get',
    params
  })
}

// 获取系总页数
export const getDepartmentPageCount = (params = {}) => {
  return request({
    url: '/api/admin/department/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑系
export const saveDepartment = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/department',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/department',
      method: 'put',
      data
    })
  }
}

// 删除系
export const deleteDepartment = (id) => {
  return request({
    url: `/api/admin/department/${id}`,
    method: 'delete'
  })
}

// 通过ID获取系信息
export const getDepartmentById = (id) => {
  return request({
    url: `/api/admin/department/${id}`,
    method: 'get'
  })
}

// 获取系名称列表
export const getDepartmentNames = () => {
  return request({
    url: '/api/admin/department/names',
    method: 'get'
  })
}

// ==================== 专业管理 ====================
// 获取专业列表（分页）
export const getMajorPage = (page, params = {}) => {
  return request({
    url: `/api/admin/major/page/${page}`,
    method: 'get',
    params
  })
}

// 获取专业总页数
export const getMajorPageCount = (params = {}) => {
  return request({
    url: '/api/admin/major/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑专业
export const saveMajor = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/major',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/major',
      method: 'put',
      data
    })
  }
}

// 删除专业
export const deleteMajor = (id) => {
  return request({
    url: `/api/admin/major/${id}`,
    method: 'delete'
  })
}

// 通过ID获取专业信息
export const getMajorById = (id) => {
  return request({
    url: `/api/admin/major/${id}`,
    method: 'get'
  })
}

// 获取专业名称列表
export const getMajorNames = () => {
  return request({
    url: '/api/admin/major/names',
    method: 'get'
  })
}

// ==================== 班级管理 ====================
// 获取班级列表（分页）
export const getClassPage = (page, params = {}) => {
  return request({
    url: `/api/admin/class/page/${page}`,
    method: 'get',
    params
  })
}

// 获取班级总页数
export const getClassPageCount = (params = {}) => {
  return request({
    url: '/api/admin/class/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑班级
export const saveClass = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/class',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/class',
      method: 'put',
      data
    })
  }
}

// 删除班级
export const deleteClass = (id) => {
  return request({
    url: `/api/admin/class/${id}`,
    method: 'delete'
  })
}

// 通过ID获取班级信息
export const getClassById = (id) => {
  return request({
    url: `/api/admin/class/${id}`,
    method: 'get'
  })
}

// 获取班级名称列表
export const getClassNames = () => {
  return request({
    url: '/api/admin/class/names',
    method: 'get'
  })
}

// ==================== 学生管理 ====================
// 获取学生列表（分页）
export const getStudentPage = (page, params = {}) => {
  return request({
    url: `/api/admin/student/page/${page}`,
    method: 'get',
    params
  })
}

// 获取学生总页数
export const getStudentPageCount = (params = {}) => {
  return request({
    url: '/api/admin/student/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑学生
export const saveStudent = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/student',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/student',
      method: 'put',
      data
    })
  }
}

// 删除学生
export const deleteStudent = (id) => {
  return request({
    url: `/api/admin/student/${id}`,
    method: 'delete'
  })
}

// 通过ID获取学生信息
export const getStudentById = (id) => {
  return request({
    url: `/api/admin/student/${id}`,
    method: 'get'
  })
}

// 获取学生名称列表
export const getStudentNames = () => {
  return request({
    url: '/api/admin/student/names',
    method: 'get'
  })
}

// ==================== 教师管理 ====================
// 获取教师列表（分页）
export const getTeacherPage = (page, params = {}) => {
  return request({
    url: `/api/admin/teacher/page/${page}`,
    method: 'get',
    params
  })
}

// 获取教师总页数
export const getTeacherPageCount = (params = {}) => {
  return request({
    url: '/api/admin/teacher/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑教师
export const saveTeacher = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/teacher',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/teacher',
      method: 'put',
      data
    })
  }
}

// 删除教师
export const deleteTeacher = (id) => {
  return request({
    url: `/api/admin/teacher/${id}`,
    method: 'delete'
  })
}

// 通过ID获取教师信息
export const getTeacherById = (id) => {
  return request({
    url: `/api/admin/teacher/${id}`,
    method: 'get'
  })
}

// 获取教师名称列表
export const getTeacherNames = () => {
  return request({
    url: '/api/admin/teacher/names',
    method: 'get'
  })
}

// ==================== 课程管理 ====================
// 获取课程列表（分页）
export const getCoursePage = (page, params = {}) => {
  return request({
    url: `/api/admin/course/page/${page}`,
    method: 'get',
    params
  })
}

// 获取课程总页数
export const getCoursePageCount = (params = {}) => {
  return request({
    url: '/api/admin/course/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑课程
export const saveCourse = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/course',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/course',
      method: 'put',
      data
    })
  }
}

// 删除课程
export const deleteCourse = (id) => {
  return request({
    url: `/api/admin/course/${id}`,
    method: 'delete'
  })
}

// 通过ID获取课程信息
export const getCourseById = (id) => {
  return request({
    url: `/api/admin/course/${id}`,
    method: 'get'
  })
}

// 获取课程名称列表
export const getCourseNames = () => {
  return request({
    url: '/api/admin/course/names',
    method: 'get'
  })
}

// ==================== 选课管理 ====================
// 获取选课列表（分页）
export const getSelectionPage = (page, params = {}) => {
  return request({
    url: `/api/admin/student/course/page/${page}`,
    method: 'get',
    params
  })
}

// 获取选课总页数
export const getSelectionPageCount = (params = {}) => {
  return request({
    url: '/api/admin/student/course/page/count',
    method: 'get',
    params
  })
}

// 添加/编辑选课
export const saveSelection = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/student/course',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/student/course',
      method: 'put',
      data
    })
  }
}

// 删除选课
export const deleteSelection = (id) => {
  return request({
    url: `/api/admin/student/course/${id}`,
    method: 'delete'
  })
}

// 通过ID获取选课信息
export const getSelectionById = (id) => {
  return request({
    url: `/api/admin/student/course/${id}`,
    method: 'get'
  })
}

// ==================== 管理员管理 ====================
// 获取管理员列表
export const getAdminList = () => {
  return request({
    url: '/api/admin/admin',
    method: 'get'
  })
}

// 获取管理员列表（别名，兼容）
export const getAdminPage = () => {
  return request({
    url: '/api/admin/admin',
    method: 'get'
  })
}

// 添加/编辑管理员
export const saveAdmin = (data) => {
  if (data.id === -1) {
    return request({
      url: '/api/admin/admin',
      method: 'post',
      data
    })
  } else {
    return request({
      url: '/api/admin/admin',
      method: 'put',
      data
    })
  }
}

// 删除管理员
export const deleteAdmin = (id) => {
  return request({
    url: `/api/admin/admin/${id}`,
    method: 'delete'
  })
}

// 通过ID获取管理员信息
export const getAdminById = (id) => {
  return request({
    url: `/api/admin/admin/${id}`,
    method: 'get'
  })
}

// ==================== 教师端API ====================
// 授课查询
export const getTeacherCourseList = () => {
  return request({
    url: '/api/teacher/course/list',
    method: 'get'
  })
}

// 教师课表
export const getTeacherTimetable = () => {
  return request({
    url: '/api/teacher/timetable',
    method: 'get'
  })
}

// 成绩录入列表
export const getGradePage = (page, params = {}) => {
  return request({
    url: `/api/teacher/grade/page/${page}`,
    method: 'get',
    params
  })
}

// 成绩录入条数
export const getGradePageCount = (params = {}) => {
  return request({
    url: '/api/teacher/grade/page/count',
    method: 'get',
    params
  })
}

// 成绩录入详情
export const getGradeById = (id) => {
  return request({
    url: `/api/teacher/grade/${id}`,
    method: 'get'
  })
}

// 成绩录入打分
export const saveGrade = (data) => {
  if (process.env.NODE_ENV !== 'production') {
    console.debug('[API][saveGrade][REQ]', data)
  }
  return request({
    url: '/api/teacher/grade',
    method: 'put',
    data
  })
}

// ==================== 学生端（自助）API ====================
// 选修课程分页（学生可选课程）
export const getStudentSelectableCoursePage = (page, params = {}) => {
  return request({
    url: `/api/student/course/select/page/${page}`,
    method: 'get',
    params
  })
}

// 学生选课执行
export const studentSelectCourse = (courseId) => {
  return request({
    url: `/api/student/course/select/${courseId}`,
    method: 'post'
  })
}

// 学生退选
export const studentDropCourse = (selectionId) => {
  return request({
    url: `/api/student/course/${selectionId}`,
    method: 'delete'
  })
}

// 学生课程 / 成绩列表（同一接口）
export const getStudentCourseList = () => {
  return request({
    url: '/api/student/course/list',
    method: 'get'
  })
}

// 学生课表
export const getStudentTimetable = () => {
  return request({
    url: '/api/student/timetable',
    method: 'get'
  })
}

// 学生个人信息
export const getStudentInfo = () => {
  return request({
    url: '/api/student/info',
    method: 'get'
  })
}